package com.example.myapp.ADO;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.myapp.Activity.VolleyChat;
import com.example.myapp.Model.Msg;
import com.example.myapp.Model.MsgList;
import com.example.myapp.Model.User;
import com.example.myapp.R;
import com.loopj.android.image.SmartImageView;

import java.util.List;

import cn.bmob.v3.BmobUser;
import de.hdodenhof.circleimageview.CircleImageView;

public class Chatlist_Adapter extends RecyclerView.Adapter<Chatlist_Adapter.ViewHolder>{
    private List<MsgList> mMsgList;
    private Context context;
    public class ViewHolder extends RecyclerView.ViewHolder {

        CircleImageView myfrend_icon ;
        TextView myfrendusernmae ;
        TextView myfrendjianjie ;
        TextView myfrendmsgcount ;
        LinearLayout myfrendlayoutbtn;

        public ViewHolder(View view) {
            super(view);
            myfrend_icon = view.findViewById(R.id.myfrend_icon);
             myfrendusernmae = view.findViewById(R.id.myfrendusernmae);
             myfrendjianjie = view.findViewById(R.id.myfrendjianjie);
             myfrendmsgcount = view.findViewById(R.id.myfrendmsgcount);
             myfrendlayoutbtn= view.findViewById(R.id.myfrendlayoutbtn);



        }
    }

    public Chatlist_Adapter(Context context,List<MsgList> msgList) {
        this.mMsgList = msgList;
        this. context=context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_listchat,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int i) {
        Glide.with(context).load( mMsgList.get(i).getIcon()).into(holder.myfrend_icon);// CircleImageView.显示网络图片特殊方法
        holder. myfrendusernmae.setText( ""+mMsgList.get(i).getUsername());
        holder. myfrendjianjie.setText(""+mMsgList.get(i).getMsg());
        holder.  myfrendlayoutbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, VolleyChat.class);
                intent.putExtra("userID",mMsgList.get(i).getUserid());
                intent.putExtra("iconUrl",mMsgList.get(i).getIcon());
                intent.putExtra("userName",mMsgList.get(i).getUsername());
                intent.putExtra("isUserPagecome","MsgList");
                 context.startActivity(intent);
            }
        });
        if(mMsgList.get(i).getNum()>0){
            holder.myfrendmsgcount.setVisibility(View.VISIBLE);
            holder. myfrendmsgcount.setText(""+mMsgList.get(i).getNum());
        }


    }

    @Override
    public int getItemCount() {
        return mMsgList.size();
    }
}